<?php
namespace Home\Model;
use Think\Verify;
use Think\Model;
class RechargeModel extends Model 
{
	public $tableName = 'recharge';
	
	public function random()
	{
		return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
	}	

	//充值积分
	public function buyTo($arr)
	{
		

		$shopModel = M('shop');
		$shop['phone'] = $arr['phone'];
		$shop['isApply'] = 1;
		$shop['apply'] = 1;
		$shopRes      = $shopModel->where($shop)->select();
		//var_dump($shopRes);exit();
		if($shopRes)
		{
			//需要支付的金额
			$num = $arr['num'];
			$money = $num/10;
			$random = $this->random();
			$pay['id'] = $random;
			$pay['money'] = $money;
			$pay['shop_id'] = $shopRes[0]['shop_id'];
			$pay['shijian'] = date('Y-m-d H:i:s',time());

			$payModel = M('pay');
			$res_in = $payModel->data($pay)->add();
			//var_dump($res_in);exit();
			if($res_in)
			{
				$rechargeModel = M($this->tableName);
				$recharge['points'] = $arr['num'];
				$recharge['reason'] = '购买积分';
				$recharge['shop_id'] = $shopRes[0]['shop_id'];
				$recharge['shijian'] = date('Y-m-d H:i:s',time());
				$res = $rechargeModel->data($recharge)->add();
				if($res)
				{
					$points = D('Shop');
					$points->points($recharge['shop_id']);
					return 'success';
				}else
				{
					return 'noRecharge';//充值日志
				}

			}else
			{
				return 'noPay';//支付失败
			}
		}else
		{
			return 'noShop';//商店不存在
		}
		
	}


}